The claimed amount data ― 5 ― does not agree with the amount of data present.
It might have been better to write the program using a sentinel-controlled loop
where the loop uses hasNextInt()
before it attempts to read in
another value.
However, sometimes a counting loop must be used with input. The data sometimes consist of several groups where each group starts with a header that says how much data is in that group. For example:
There are two groups of computer science students. The first group (called group "A") uses on-line lessons. The other group (called group "B") uses traditional printed text. All the students are given the same mid-term examination. Which group has the higher test average?
The file of test scores looks like this (the blue comments are not in the file):
3 <-- number of students in group "A" 87 98 95 4 <-- number of students in group "B" 78 82 91 84
Group "A" has three students in it and group "B" has four students in it. The program is to compute two averages from the data in this file.